package cn.cxy.wanandroid.base

import android.os.Bundle
import android.view.View
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import cn.cxy.wanandroid.R
import cn.cxy.wanandroid.views.LoadingDialog
import com.shehuan.statusview.StatusView
import com.shehuan.statusview.StatusViewBuilder
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *@创建者wwy
 *@创建时间 2019/10/9 11:08
 *@描述
 */
abstract class BaseActivity : AppCompatActivity() {
    lateinit var mContext: BaseActivity
    //    private lateinit var progressDialogFragment: ProgressDialogFragment
    override fun onCreate(savedInstanceState: Bundle?) {
//        setTheme(getAppTheme())
        super.onCreate(savedInstanceState)
        mContext = this
        initContentView()
        setContentView(setLayoutId())
        initImmersionBar()
        setSmartSnackBar()
        initActivity(savedInstanceState)
        initLoad()
    }
    open fun initLoad(){}
    open fun initContentView() {}

    protected fun initToolbar(@StringRes titleId: Int) {
        initToolbar(getString(titleId))
    }

    protected fun initToolbar(titleStr: String) {
        toolbar.run {
            title = titleStr
            setSupportActionBar(this)
            setNavigationOnClickListener {
                finish()
            }
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
    }

    open fun setSmartSnackBar() {
//        SmartSnackbar.setting()
//            .backgroundColorRes(
//                TypedValue().resourceId(
//                    R.attr.colorAccent,
//                    theme
//                )
//            )
//            .dismissOnLeave(true)
    }

    protected open fun initActivity(savedInstanceState: Bundle?) {
        initView(savedInstanceState)
        initListeners()
        initData()
    }

    abstract fun setLayoutId(): Int
    abstract fun initView(savedInstanceState: Bundle?)

    open fun initView(){}
    open fun initListeners() {}
    abstract fun initData()
    open fun initImmersionBar() {
//        immersionBar {
//            statusBarColor(
//                TypedValue().resourceId(
//                    R.attr.colorPrimary,
//                    theme
//                )
//            ).autoStatusBarDarkModeEnable(true, 0.2f)
//        }
    }

    /**
     * 显示加载(转圈)对话框
     */
    fun showProgressDialog(@StringRes message: Int) {
//        if (!this::progressDialogFragment.isInitialized) {
//            progressDialogFragment = ProgressDialogFragment.newInstance()
//        }
//        if (!progressDialogFragment.isAdded) {
//            progressDialogFragment.show(supportFragmentManager, message, false)
//        }
    }

    /**
     * 隐藏加载(转圈)对话框
     */
    fun dismissProgressDialog() {
//        if (this::progressDialogFragment.isInitialized && progressDialogFragment.isVisible) {
//            progressDialogFragment.dismissAllowingStateLoss()
//        }
    }

    protected lateinit var statusView: StatusView

    protected fun initStatusView(id: Int, errorRetry: (View) -> Unit) {
        statusView = StatusView.init(this, id).apply {
            setLoadingView(R.layout.dialog_loading_layout)
            config(
                StatusViewBuilder.Builder()
                    .showEmptyRetry(false)
                    .setOnErrorRetryClickListener(errorRetry)
                    .build()
            )
        }
    }

    private var loadingDialog: LoadingDialog? = null

    protected fun showLoading() {
        if (loadingDialog == null || loadingDialog?.dialog == null) {
            loadingDialog = LoadingDialog.newInstance()
            loadingDialog!!.show(supportFragmentManager)
        }
    }

    protected fun hideLoading() {
        if (loadingDialog != null) {
            loadingDialog!!.dismiss()
        }
    }

}